//
//  WFSettingViewController.m
//  WisdomFarm
//
//  Created by sky on 2017/10/24.
//  Copyright © 2017年 sky. All rights reserved.
//

#import "WFSettingViewController.h"
#import "WFSeparatorTableViewCell.h"
#import "WFSettingHeaderView.h"
#import "ResetPwdViewController.h"
#import "WFAboutUsViewController.h"
#import "WFPasswordModifyViewController.h"

@interface WFSettingEntity : NSObject

@property (nonatomic, strong) NSString *title;
@property (nonatomic, strong) NSString *imageName;
@property (nonatomic, copy) void (^clickHandle)(UITableViewCell *cell, NSIndexPath *indexPath);

+ (instancetype)entityWithTitle:(NSString *)title image:(NSString *)imageName handle:(void (^)(UITableViewCell *cell, NSIndexPath *indexPath))handle;

@end

@implementation WFSettingEntity

+ (instancetype)entityWithTitle:(NSString *)title image:(NSString *)imageName handle:(void (^)(UITableViewCell *cell, NSIndexPath *indexPath))handle {
    WFSettingEntity *entity = [WFSettingEntity new];
    entity.title = title;
    entity.imageName = imageName;
    entity.clickHandle = handle;
    return entity;
}

@end

@interface WFSettingViewController ()

@property (nonatomic, strong) NSArray *entities;
@property (nonatomic, strong) WFSettingHeaderView *headerView;
@property (nonatomic, strong) WFSettingEntity *modifyEntity;
@property (nonatomic, strong) WFSettingEntity *passwordEntity;
@property (nonatomic, strong) WFSettingEntity *settingEntity;
@property (nonatomic, strong) WFSettingEntity *separatorEntity;
@property (nonatomic, strong) WFSettingEntity *readmeEntity;
@property (nonatomic, strong) WFSettingEntity *aboutEntity;
@property (nonatomic, strong) WFSettingEntity *linkEntity;
@property (nonatomic, strong) WFSettingEntity *logoutEntity;

@end

@implementation WFSettingViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication].currentNavigationController setNavigationBarHidden:YES];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if (IS_IOS_11) {
        self.tableView.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
        self.tableView.contentInset = UIEdgeInsetsMake(0, 0, 46, 0);
    }
    
    @weakify(self);
    [self.navigationController setNavigationBarHidden:YES];
    
    [self.tableView registerClass:[WFSeparatorTableViewCell class] forCellReuseIdentifier:@"WFSeparatorTableViewCell"];
    [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"CELL"];
    self.tableView.rowHeight = 70;
    self.tableView.separatorColor = [UIColor separatorColor];
    self.tableView.backgroundColor = [UIColor backgroundColor];
    self.headerView = [[WFSettingHeaderView alloc] initWithFrame:CGRectMake(0, 0, self.view.width, 222)];
    self.tableView.tableHeaderView = self.headerView;
    [self.headerView.avatarButton bk_addEventHandler:^(id sender) {
        @strongify(self);
        if ([WFStore currentUser]) {
//            [WFRouter toUserEdit];
        } else {
            [WFRouter toLoginFinished:^(BOOL result) {
                if (!result) {
                    return;
                }
                [self refreshAll];
                [self dismissViewControllerAnimated:YES completion:nil];
            }];
        }
    } forControlEvents:UIControlEventTouchUpInside];

    
    WFSettingEntity *modifyEntity = [WFSettingEntity entityWithTitle:@"修改资料" image:@"about_edit" handle:^(UITableViewCell *cell, NSIndexPath *indexPath) {
        //修改资料
        [WFRouter toUserEdit];
    }];
    self.modifyEntity = modifyEntity;
    
    WFSettingEntity *passwordEntity = [WFSettingEntity entityWithTitle:@"修改密码" image:@"about_modify" handle:^(UITableViewCell *cell, NSIndexPath *indexPath) {
        //修改密码
        [WFRouter loginIfNeededWithFinishHandle:^(BOOL result) {
            if (!result) {
                return;
            }
            WFPasswordModifyViewController *controller = [WFPasswordModifyViewController new];
            [[UIApplication sharedApplication] sc_pushViewController:controller animated:YES];
        }];
    }];
    self.passwordEntity = passwordEntity;
    
    WFSettingEntity *settingEntity = [WFSettingEntity entityWithTitle:@"设置" image:@"about_setting" handle:^(UITableViewCell *cell, NSIndexPath *indexPath) {
        //TODO: 设置
    }];
    self.settingEntity = settingEntity;
    
    self.separatorEntity = [WFSettingEntity new];
    
    WFSettingEntity *readmeEntity = [WFSettingEntity entityWithTitle:@"使用说明" image:@"about_readme" handle:^(UITableViewCell *cell, NSIndexPath *indexPath) {
        //TODO:使用说明
    }];
    self.readmeEntity = readmeEntity;
    
    WFSettingEntity *aboutEntity = [WFSettingEntity entityWithTitle:@"关于我们" image:@"about_me" handle:^(UITableViewCell *cell, NSIndexPath *indexPath) {
        //关于我们
        WFAboutUsViewController *controller = [[WFAboutUsViewController alloc] init];
        [[UIApplication sharedApplication] sc_pushViewController:controller animated:YES];
    }];
    self.aboutEntity = aboutEntity;
    
    WFSettingEntity *linkEntity = [WFSettingEntity entityWithTitle:@"友情链接" image:@"about_link" handle:^(UITableViewCell *cell, NSIndexPath *indexPath) {
        //TODO:友情链接
    }];
    self.linkEntity = linkEntity;
    
    WFSettingEntity *logoutEntity = [WFSettingEntity entityWithTitle:@"退出登录" image:@"about_logout" handle:^(UITableViewCell *cell, NSIndexPath *indexPath) {
        @strongify(self);
        [WFStore setCurrentUser:nil];
        [SVProgressHUD showSuccessWithStatus:@"退出成功"];
        [self refreshAll];
    }];
    self.logoutEntity = logoutEntity;
    
    [self refreshAll];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)refreshAll {
    WFUser *user = [WFStore currentUser];
    if (user) {
        self.headerView.mobile = [WFStore currentUser].phone;
        self.entities = @[ _passwordEntity, _separatorEntity, _aboutEntity, _logoutEntity ];
//        self.entities = @[ _modifyEntity, _passwordEntity, _settingEntity, _separatorEntity, _readmeEntity, _aboutEntity, _linkEntity, _logoutEntity ];
    } else {
        self.headerView.mobile = @"";
        self.entities = @[  _passwordEntity, _separatorEntity, _aboutEntity ];
//        self.entities = @[ _modifyEntity, _passwordEntity, _settingEntity, _separatorEntity, _readmeEntity, _aboutEntity, _linkEntity ];
    }
    [self.tableView reloadData];
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.entities.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    WFSettingEntity *entity = self.entities[indexPath.row];
    if (entity == self.separatorEntity) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"WFSeparatorTableViewCell" forIndexPath:indexPath];
        return cell;
    }
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"CELL" forIndexPath:indexPath];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    cell.imageView.image = [UIImage imageNamed:entity.imageName];
    cell.textLabel.text = entity.title;
    cell.textLabel.textColor = [UIColor textContentColor];
    cell.textLabel.font = SCFont(13);
    return cell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    WFSettingEntity *entity = self.entities[indexPath.row];
    if (entity == self.separatorEntity) {
        return 12;
    }
    return 49;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    WFSettingEntity *entity = self.entities[indexPath.row];
    UITableViewCell *cell = [tableView cellForRowAtIndexPath:indexPath];
    entity.clickHandle(cell, indexPath);
}

@end
